/**
 * FileFind - ftp/shares indexing and search php engine
 * Module: common
 * Description: Common classes
 * Copyright (c) 2003-2010 Rage Steel, www.gt2.ru
 */
package ru.ffind.common.model;

import com.google.common.collect.Maps;
import java.util.Map;

/**
 * Host types
 *
 * @author Rage Steel
 */
public enum HostType {
    /**
     * FTP Resource.
     */
    FTP(1),

    /**
     * Samba Share resource.
     */
    SHARE(2),

    /**
     * DirectConnect Hub.
     * Not implemented yet.
     */
    DC_HUB(3),

    /**
     * DirectConnect User.
     * Not implemented yet.
     */
    DC_USER(4);

    private final int id;

    private HostType(int id) {
        this.id = id;
    }
    
    private static final Map<Integer, HostType> BY_ID = Maps.newHashMap();

    static {
        for (HostType hostType: HostType.values()) {
            int id = hostType.getId();
            HostType existing = BY_ID.put(id, hostType);
            if (null != existing) {
                throw new IllegalArgumentException(String.format(
                        "Duplication of id, %d is already assigned to %s",
                        id, existing));
            }
        }
    }

    /**
     * Return id for type.
     *
     * @return
     */
    public int getId() {
        return id;
    }

    /**
     * Return hostType by id.
     */
    public static HostType valueOf(int id) {
        HostType result = BY_ID.get(id);
        if (null == result) {
            throw new IllegalArgumentException(String.format("HostType is not "
                    + "defined for id %d", id));
        }
        return result;
    }
}